home *** CD-ROM | disk | FTP | other *** search
/ Imagine the Universe (9th Edition) / Imagine the Universe 9 - Disc 1.iso / pc / imagine / scripts / js_util.js < prev    next >
Text File  |  2002-05-21  |  6KB  |  269 lines

  1. // The JavaScript Junkyard
  2. // interesting and semi-useful JS snippets
  3. // from scottandrew.com/junkyard/js/
  4. // copyright 2001 scott andrew lepera
  5.  
  6. // getDistance()
  7. // Returns the average distance between two specified points (usually for pixels)
  8. function getDistance(x1,y1,x2,y2)
  9. {
  10.   var d =  Math.round(Math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))));
  11.     if (isNaN(d)) d = 0;
  12.   return d;
  13. }
  14.  
  15. // sortByProperty()
  16. // Sort the array order based on a specific property of each array element,
  17. //in forward or reverse order.
  18. function _sortByProperty(property,rev)
  19. {
  20.   var fn = function(a,b)
  21.   {
  22.     if (a[property] < b[property])
  23.     {
  24.       return (rev)? 1:-1;
  25.     } else if (a[property] > b[property])
  26.     {
  27.       return (rev)? -1:1;
  28.     } else
  29.     {
  30.       return 0;
  31.     }
  32.   }
  33.   this.sort(fn);
  34. }
  35.  
  36. Array.prototype.sortByProperty = _sortByProperty;
  37.  
  38. // getAngle()
  39. // Gives the angle of the radian described by the two specified points
  40. function getAngle(x1,y1,x2,y2)
  41. {
  42.   var diffH = (x1-x2);
  43.   var diffV = (y2-y1);
  44.   if (diffH){
  45.     var slope = diffV / diffH ;
  46.     var angle = Math.atan(slope);
  47.     var dgrs = (angle * 180) / Math.PI;
  48.       if (diffH < 0){dgrs += 180;}
  49.   }
  50.   else if (diffV < 0){dgrs = 270;}
  51.   else if (diffV > 0){dgrs = 90;}
  52.   else {dgrs = 0;}
  53.   if (dgrs < 0) dgrs = 360 + dgrs;
  54.   return Math.round(dgrs);
  55. }
  56.  
  57. // roundTo()
  58. // Round p to nearest n
  59. function roundTo(p,n)
  60. {
  61.     return (Math.round(n/p))*p;
  62. }
  63.  
  64. // getRandomColor()
  65. // Returns a random hex color.  Passing true for safe returns a web safe color
  66. function getRandomColor(safe)
  67. {
  68.   var vals,r,n;
  69.   if (safe)
  70.   {
  71.     v = "0369cf";
  72.     n = 3;
  73.   } else
  74.   {
  75.     v = "0123456789abcdef";
  76.     n = 6;
  77.   }
  78.   var c = "#";
  79.   for (var i=0;i<n;i++)
  80.   {
  81.     var ch = v.charAt(Math.round(Math.random() * (v.length-1)));
  82.     c += (safe)?ch+ch:ch;
  83.   }
  84.   return c;
  85. }
  86.  
  87. // addEvent and removeEvent
  88. // cross-browser event handling for IE5+,  NS6 and Mozilla
  89.  
  90. function addEvent(elm, evType, fn, useCapture)
  91. {
  92.   if (elm.addEventListener){
  93.     elm.addEventListener(evType, fn, useCapture);
  94.     return true;
  95.   } else if (elm.attachEvent){
  96.     var r = elm.attachEvent("on"+evType, fn);
  97.     return r;
  98.   } else {
  99.     alert("Handler could not be removed");
  100.   }
  101.  
  102. function removeEvent(elm, evType, fn, useCapture)
  103. {
  104.   if (elm.removeEventListener){
  105.     elm.removeEventListener(evType, fn, useCapture);
  106.     return true;
  107.   } else if (elm.detachEvent){
  108.     var r = elm.detachEvent("on"+evType, fn);
  109.     return r;
  110.   } else {
  111.     alert("Handler could not be removed");
  112.   }
  113. }
  114.  
  115. // Cookie handling functions
  116.  
  117. function saveCookie(name,value,days)
  118. {
  119.   if (days) {
  120.     var d = new Date();
  121.     d.setTime(d.getTime()+(days*24*60*60*1000));
  122.     var ex = "; expires="+date.toGMTString();
  123.   }
  124.   else {
  125.     var ex = "";
  126.   }
  127.   document.cookie = name+"="+value+ex+"; path=/";
  128. }
  129.  
  130. function readCookie(name)
  131. {
  132.   var eq = name + "=";
  133.   var ca = document.cookie.split(';');
  134.   for(var i=0;i<ca.length;i++) {
  135.     var c = ca[i];
  136.     while (c.charAt(0)==' ') c = c.substring(1,c.length);
  137.     if (c.indexOf(eq) == 0) return c.substring(eq.length,c.length);
  138.   }
  139.   return null;
  140. }
  141.  
  142. function eraseCookie(name)
  143. {
  144.   saveCookie(name,"",-1);
  145. }
  146.  
  147. // getParams
  148.  
  149. function getQueryArgs(global)
  150. {
  151.   var args = {};
  152.   var loc = window.location.href;
  153.   var q = loc.indexOf("?");
  154.   if (q==-1) return false;
  155.   loc = loc.substring(q+1);
  156.   var pairs = loc.split("&");
  157.   for (var i=0; i<pairs.length;i++){
  158.     if (global) eval(pairs[i]);
  159.     var keyval = pairs[i].split("=");
  160.     args[keyval[0]] = unescape(keyval[1]);
  161.   }
  162.   return args;
  163. }
  164.  
  165. /* inPoly()
  166. Finds if a given point is within a polygon.
  167.  
  168. Based on Bob Stein's inpoly() function for C.
  169. http://home.earthlink.net/~bobstein/inpoly/
  170.  
  171. Modified for JavaScript by Scott Andrew LePera.
  172.  
  173. Parameters:
  174. poly: array containing x/y coordinate pairs that
  175.   describe the vertices of the polygon. Format is
  176.   indentical to that of HTML image maps, i.e. [x1,y1,x2,y2,...]
  177.   
  178. px: the x-coordinate of the target point.
  179.  
  180. py: the y-coordinate of the target point.
  181.  
  182. Return value:
  183. true if the point is within the polygon, false if not.
  184. */
  185.  
  186. function inPoly(poly,px,py)
  187. {
  188.      var npoints = poly.length; // number of points in polygon
  189.      var xnew,ynew,xold,yold,x1,y1,x2,y2,i;
  190.      var inside=false;
  191.  
  192.      if (npoints/2 < 3) { // points don't describe a polygon
  193.           return false;
  194.      }
  195.      xold=poly[npoints-2];
  196.      yold=poly[npoints-1];
  197.      
  198.      for (i=0 ; i < npoints ; i=i+2) {
  199.           xnew=poly[i];
  200.           ynew=poly[i+1];
  201.           if (xnew > xold) {
  202.                x1=xold;
  203.                x2=xnew;
  204.                y1=yold;
  205.                y2=ynew;
  206.           }
  207.           else {
  208.                x1=xnew;
  209.                x2=xold;
  210.                y1=ynew;
  211.                y2=yold;
  212.           }
  213.           if ((xnew < px) == (px <= xold) && ((py-y1)*(x2-x1) < (y2-y1)*(px-x1))) {
  214.                inside=!inside;
  215.           }
  216.           xold=xnew;
  217.           yold=ynew;
  218.      }
  219.      return inside;
  220. }
  221.  
  222. // readFile
  223. // retrieves the contents of a local file as a JS string.
  224. // NS4 requires Java enabled.
  225. // Works in Mozilla 0.9.5 +
  226.  
  227. function readFile(url)
  228. {
  229.   var req;
  230.   if (document.all){
  231.     req = new ActiveXObject("Microsoft.XMLHTTP");
  232.   }
  233.   else if (netscape){
  234.     if (document.getElementById){
  235.       req = new XMLHttpRequest();
  236.     }
  237.     else {
  238.       req = new NS4HttpRequest();
  239.     }
  240.   }
  241.   req.open("GET",url,false);
  242.   req.send(null);
  243.   return req.responseText;
  244. }
  245.  
  246. function NS4HttpRequest()
  247. {
  248.   this.url = "";
  249.   this.responseText = "";
  250. }
  251. NS4HttpRequest.prototype.open = function(method,url)
  252. {
  253.   this.url = url;
  254.   this.method = method||get;
  255. }
  256. NS4HttpRequest.prototype.send = function()
  257. {
  258.   // thank you Mr. Pemberton
  259.   if (this.url=="") return false;
  260.   var line,buffer;
  261.   this.responseText = "";
  262.   buffer = new java.io.BufferedReader(new java.io.InputStreamReader(new java.net.URL(this.url).openStream()));
  263.   while ((line = buffer.readLine())!=null) this.responseText+=line + "\n";
  264.   if (buffer!=null) buffer.close();
  265.   return true;
  266. }
  267.  
  268.